import Vue from 'vue'
import VueRouter from 'vue-router'
import login from "@/components/Login";
import index from "@/components/Index";
import ReaderIndex from "@/components/ReaderIndex";
import ReaderViews from "@/views/ReaderViews";
import LibraryViews from "@/views/LibraryViews";
import TypeViews from "@/views/TypeViews";
import NoticeViews from "@/views/NoticeViews";
import AdminViews from "@/views/AdminViews";
import LendViews from "@/views/LendViews";
import CountViews from "@/views/CountViews";
import Release from "@/components/Release";
import Register from "@/components/Register";
import StacksPage from "@/views/reader/StacksPage";
import Forum from "@/views/reader/Forum";
import PersonalCenter from "@/views/reader/personalcenter/PersonalCenter";
import FrontPage from "@/views/reader/FrontPage";
import BookShelf from "@/views/reader/BookShelf";
import Info from "@/views/reader/personalcenter/Info"
import BorrowingRecord from "@/views/reader/personalcenter/BorrowingRecord";
import ReadBooks from "@/views/reader/personalcenter/ReadBooks";
import BookDetail from "@/views/reader/BookDetail";
import BookCollection from "@/views/reader/personalcenter/BookCollection";
import MyComment from "@/views/reader/personalcenter/MyComment";
import MyPosts from "@/views/reader/personalcenter/MyPosts";
import ReleasePosts from "@/views/reader/personalcenter/ReleasePosts";
import ReaderCount from "@/views/ReaderCount";
import MyMemberEcharts from "@/views/echarts/MyMemberEcharts";
import Search from "@/views/reader/Search";
import SexCount from "@/views/SexCount";
import BorrowCount from "@/views/BorrowCount";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
    {
      path: '/login',
      component: login,
      meta: {
        title: '登录'
      }
    },
    {
      path: '/register',
      component: Register,
      meta: {
        title: '注册'
      }
    },
    {
      path: '/index',
      component: index,
      redirect: "/index/borrow_list",
      children: [
        {path:'reader_list',component: ReaderViews},
        {path:'library_list',component: LibraryViews},
          {path:'type_list',component: TypeViews},
          {path:'notice_list',name: 'notice_list',component: NoticeViews},
          {path:'admin_list',component: AdminViews},
          {path:'borrow_list',component: LendViews},
          {path:'count_list',component: CountViews},
        {path: 'reader_count',component: ReaderCount},
          {path: 'member_count',component: MyMemberEcharts},
          {path: 'sex_count',component: SexCount},
          {path: 'borrow_count',component: BorrowCount},
        {path: 'release',name: 'release', component: Release},
      ]
    },
    {
      path: '/readerIndex',
      component: ReaderIndex,
      redirect: "/readerIndex/front_page",
      children: [
        {path: 'front_page',name: 'front_page',component:FrontPage,meta: {title: '首页'}},
        {path:'stacks_page',component:StacksPage,meta: {title: '书库'}},
        {path:'forum',component:Forum,meta: {title: '论坛'}},
          {path:'bookshelf',component:BookShelf,meta: {title: '书架'}},
          {path:'book_detail',name: 'book_detail',component:BookDetail,meta: {title: '图书详情'}},
      ]
    },
    {
      path: '/personal_center',
      component: PersonalCenter,
      name: 'personal_center',
      meta: {
        title: '个人中心'
      },
      redirect: "/personal_center/info",
      children: [
        {path: 'info',name: 'info',component:Info,meta: {title: '个人信息'}},
          {path: 'borrowing_record',name: 'borrowing_record',component:BorrowingRecord,meta: {title: '借阅记录'}},
          {path: 'read_books',name: 'read_books',component:ReadBooks,meta: {title: '已读图书'}},
          {path: 'book_collection',name: 'book_collection',component:BookCollection,meta: {title: '图书收藏'}},
          {path: 'my_comment',name: 'my_comment',component:MyComment,meta: {title: '我的评论'}},
          {path: 'my_posts',name: 'my_posts',component:MyPosts,meta: {title: '我的帖子'}},
          {path: 'release_posts',name: 'release_posts',component:ReleasePosts,meta: {title: '发布帖子'}},
      ]
    },
    {
      path: '/search',
      name: 'search',
      component: Search,
      meta: {
        title: '搜索'
      }
    },
]

const router = new VueRouter({
  mode: 'history',
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.title){
    document.title=to.meta.title
  }
  // 检查是否是登录或注册页面
  if (to.path==='/login'|| to.path === '/register'){
    return next()
  }else {
    let token = window.localStorage.getItem('token')
    if (token){
      return next()
    }else {
      Vue.prototype.$message.info("请先登录")
      return next('/login')
    }
  }
})

const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

export default router
